package com.hutao.manage.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hutao.manage.pojo.LoginPojo;
import com.hutao.manage.pojo.User;
import com.hutao.manage.service.UserService;
import com.hutao.manage.util.CookieUtils;
import com.hutao.manage.util.Result;

/**
 * 用户登录控制器
 * 
 * @author h2oco2
 *
 */
@RequestMapping("/user")
@Controller
public class LoginController {
	
	@Autowired
	private UserService userService;

	
	@GetMapping("/toLogin")
	public String toLogin() {
		return "/login";
	}

	@RequestMapping("/error")
	public String error() {
		return "/error";
	}

	/**
	 * 新系统
	 * 用户的登录
	 * 
	 * @param loginName
	 * @param password
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping("/userLogin")
	public String login(String loginName, String password,String orgCode, HttpServletRequest request,
			HttpServletResponse response) {

		try {
			String contextPath = request.getContextPath();
			int index = contextPath.lastIndexOf("/");
			contextPath = contextPath.substring(index+1);
			
			Result result = userService.login(loginName, password, orgCode);
			if (result.isSuccess()) {
				Subject subject = SecurityUtils.getSubject();
				UsernamePasswordToken token = new UsernamePasswordToken(loginName,password);
				LoginPojo loginPojo = (LoginPojo) result.getData();
				HttpSession session = request.getSession();
				session.setAttribute("orgCode", orgCode);
				User user = new User();
				subject.login(token);
				user.setName(loginPojo.getName());
				user.setOrgCode(orgCode);
				session.setAttribute("user", user);
	
				return "redirect:/page/index";
			}
			request.setAttribute("loginName", loginName);
			request.setAttribute("password", password);
			request.setAttribute("errorMsg", result.getMsg());
			
			return "/login";
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("loginName", loginName);
			request.setAttribute("password", password);
			request.setAttribute("errorMsg", e.getMessage());
			return "/login";
		}
		
	}
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request, HttpServletResponse response) {
		//获取shiro的subiect获取session
		HttpSession session = request.getSession();
		String orgCode = (String) session.getAttribute("orgCode");
//		//获取cookie
//		Cookie[] cookies = request.getCookies();
//		//清空cookie
//		if (null != cookies) {
//			for (Cookie cookie : cookies) {
//				String cookieName = cookie.getName();
//				String cookieValue = cookie.getValue();
//				if (cookieName.contains("TT_TOKEN") && null != cookieValue) {
//					CookieUtils.deleteCookie(request, response, cookieName);
//					CookieUtils.deleteCookie(request, response, "userName");
//				}
//
//			}
//		}
		//shiro退出
		session.setAttribute("user", null);
		return "/login";
	}

	
	
	/**
	 * 没有登录的用户跳转到此方法
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/index")
	public String index(HttpServletRequest request) {
		return "/index";
	}

	
	
}
